function distributiontablePositioner(e)
{
  if(search_results = document.getElementById('search_results'))
  {
    startpos = search_results.offsetTop;
    correction = (window.pageYOffset - startpos < 0) ? 0 : window.pageYOffset-startpos;
    if(correction !== 0)
    {
      middelcorrection = $(window).height()/2 - $('#search_result_distro').height()/2;
    }
    else
    {
      middelcorrection = $(window).height()/2 - $('#search_result_distro').height()/2 - (startpos - window.pageYOffset);
    }

    $('#search_result_distro').css('marginTop', correction + middelcorrection +"px");
  }
}

function show_tooltip(e)
{
  var tooltip = document.createElement("div");
  tooltip.id = "tooltip";
  tooltip.style.position = "fixed";
  tooltip.style.left = e.clientX+"px";
  tooltip.style.top = e.clientY+"px";
  tooltip.style.height = "310px";
  tooltip.style.width = "223px";

  var n = this.id.split("|");

  //get image
  $.get("./php_scripts/get_card_image.php?cardid="+n[0]+"&cardside="+n[1], function(data) {
    $('#tooltip').css('background-image', 'url("' + data + '")');
  });

  //get distribution
  ajaxRequest_distribution = new XMLHttpRequest();
  ajaxRequest_distribution.onreadystatechange = function()
  {
    if(ajaxRequest_distribution.readyState === 4)
    {
      if(ajaxDisplay = document.getElementById('search_result_distro'))
      {
        ajaxDisplay.innerHTML=ajaxRequest_distribution.responseText;
        distributiontablePositioner();
      }
    }
  };
  ajaxRequest_distribution.open("GET", "./php_scripts/getDistribution.php?cardid="+this.id+"&incrementors=", true);
  ajaxRequest_distribution.send(null);

  $('body').append(tooltip);
}

function replace_tooltip(e)
{
  correction = (e.clientY + 310 > document.documentElement.clientHeight) ? ((e.clientY+310) - document.documentElement.clientHeight) : 0;

  $('#tooltip').css("left", e.clientX+10+"px"); 
  $('#tooltip').css('top', e.clientY+10 - correction +"px");
}

function close_tooltip(e)
{
  document.body.removeChild(document.getElementById("tooltip"));
  $('#search_result_distro').html("<h4>Hover over a cardname.</h4>");
  distributiontablePositioner();
}

function initialize_tooltip()
{
  $('.cardname').mouseover(show_tooltip);
  $('.cardname').mousemove(replace_tooltip);
  $('.cardname').mouseout(close_tooltip);

  window.onscroll = distributiontablePositioner;

  distributiontablePositioner();
}